using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Comteck.Winforms.Print {
  public class ImageHelper {
    /// <summary>         
    /// 重新设置图片尺寸     
    /// </summary> 
    /// <param name="srcbitmap">original Bitmap</param>         
    /// <param name="newW">new width</param>         
    /// <param name="newH">new height</param>         
    /// <returns>worked bitmap</returns> 
    public static Bitmap ResizeImage(Bitmap srcimg, int newW, int newH) {
      if (srcimg == null) {
        throw new Exception("原始图片不存在！");
      }

      Bitmap destimage = new Bitmap(newW, newH);
      Graphics graph = Graphics.FromImage(destimage);
      try {
        graph.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graph.DrawImage(srcimg, new Rectangle(0, 0, newW, newH),
            new Rectangle(0, 0, srcimg.Width, srcimg.Height),
            GraphicsUnit.Pixel);

        graph.Dispose();
      } catch /*(Exception ex)*/
        {

      }
      return destimage;
    }


    /// <summary>
    /// 装载图像文件
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static Image LoadImage(String filename) {
      //Boolean bl = FileProcessor.FileExist(filename);
      //if (!bl)
      //{
      //    return null;
      //}
      //Bitmap image = (Bitmap)Bitmap.FromFile(filename);

      //return image;

      //以上方法会导致图片文件被锁定,无法删除移动

      Byte[] photodata = null;
      bool bl = File.Exists(filename);
      if (!bl) {
        return null;
      }

      photodata = File.ReadAllBytes(filename);
      if (photodata == null || photodata.Count() < 1) {
        return null;
      }

      MemoryStream ms = null;
      Image myImage = null;
      try {
        ms = new MemoryStream(photodata);
        myImage = Bitmap.FromStream(ms);
        ms.Close();
      } catch (System.Exception ex) {
        Console.WriteLine("LoadImage error:" + ex.Message);
        myImage = null;
      }
      return myImage;
    }


    /// <summary>
    /// 把图片转换为非黑即白的二色图.
    /// </summary>
    /// <param name="bitmap">原始图</param>
    /// <param name="brightnessGate">亮度门限.超过此亮度认为白点,否则认为黑点</param>
    /// <param name="bitary">每个像素点是否为黑点的数组</param>
    /// <param name="trueAsblack">true-每个元素黑点为true,白点为false; false-每个元素白点为true,黑点为false</param>
    /// <returns></returns>
    public static Boolean ToBooleanArray(Bitmap bitmap, Byte brightnessGate, ref Boolean[] bitary, Boolean trueAsblack = true) {
      if (bitmap == null) {
        return false;
      }

      bitary = new Boolean[bitmap.Width * bitmap.Height];

      int width = bitmap.Width;
      int height = bitmap.Height;

      byte curcolor = 0;
      Int32 pixidx = 0;
      Boolean bl = false;
      try {
        BitmapData srcData = bitmap.LockBits(new Rectangle(0, 0, width, height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        unsafe {
          byte* curpix = (byte*)srcData.Scan0.ToPointer();

          for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
              curcolor = (byte)((float)(curpix[0] + curpix[1] + curpix[2]) / 3.0f);

              if (trueAsblack)//true为黑点
              {
                bitary[pixidx] = (curcolor < brightnessGate);
              } else {
                //true为白点
                bitary[pixidx] = (curcolor > brightnessGate);
              }
              ++pixidx;
              curpix += 3;
            }
            curpix += srcData.Stride - width * 3;
          }
          bitmap.UnlockBits(srcData);
        }

        bl = true;
      } catch (Exception ex) {
        Console.WriteLine("ToGray error:" + ex.Message);
        bl = false;
      }

      return bl;
    }


  }
}

